package com.tencent.smtt.utils;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import java.io.File;
import java.io.InputStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.security.cert.Certificate;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

/* loaded from: classes2.dex */
public class b {
    public static String a = "";
    public static String b = "";
    public static String c = "";
    public static String d = "";
    public static String e = "";

    private static PackageInfo a(String str, int i) {
        Class<?> cls;
        try {
            Class<?> cls2 = Class.forName("android.content.pm.PackageParser");
            Class<?>[] declaredClasses = cls2.getDeclaredClasses();
            int length = declaredClasses.length;
            int i2 = 0;
            while (true) {
                if (i2 >= length) {
                    cls = null;
                    break;
                }
                cls = declaredClasses[i2];
                if (cls.getName().compareTo("android.content.pm.PackageParser$Package") == 0) {
                    break;
                }
                i2++;
            }
            Constructor<?> constructor = cls2.getConstructor(String.class);
            Method declaredMethod = cls2.getDeclaredMethod("parsePackage", File.class, String.class, DisplayMetrics.class, Integer.TYPE);
            Method declaredMethod2 = cls2.getDeclaredMethod("collectCertificates", cls, Integer.TYPE);
            Method declaredMethod3 = cls2.getDeclaredMethod("generatePackageInfo", cls, int[].class, Integer.TYPE, Long.TYPE, Long.TYPE);
            constructor.setAccessible(true);
            declaredMethod.setAccessible(true);
            declaredMethod2.setAccessible(true);
            declaredMethod3.setAccessible(true);
            Object newInstance = constructor.newInstance(str);
            DisplayMetrics displayMetrics = new DisplayMetrics();
            displayMetrics.setToDefaults();
            Object invoke = declaredMethod.invoke(newInstance, new File(str), str, displayMetrics, 0);
            if (invoke == null) {
                return null;
            }
            if ((i & 64) != 0) {
                declaredMethod2.invoke(newInstance, invoke, 0);
            }
            return (PackageInfo) declaredMethod3.invoke(null, invoke, null, Integer.valueOf(i), 0, 0);
        } catch (Exception e2) {
            e2.printStackTrace();
            return null;
        }
    }

    /* JADX WARN: Code restructure failed: missing block: B:29:0x0062, code lost:
        if (r3 == null) goto L23;
     */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct code enable 'Show inconsistent code' option in preferences
    */
    public static java.lang.String a() {
        /*
            java.lang.String r0 = "os.arch"
            java.lang.String r1 = com.tencent.smtt.utils.b.c
            boolean r1 = android.text.TextUtils.isEmpty(r1)
            if (r1 != 0) goto Ld
            java.lang.String r0 = com.tencent.smtt.utils.b.c
            return r0
        Ld:
            r1 = 0
            java.lang.Runtime r2 = java.lang.Runtime.getRuntime()     // Catch: java.lang.Throwable -> L4c
            java.lang.String r3 = "getprop ro.product.cpu.abi"
            java.lang.Process r2 = r2.exec(r3)     // Catch: java.lang.Throwable -> L4c
            java.io.InputStreamReader r3 = new java.io.InputStreamReader     // Catch: java.lang.Throwable -> L4c
            java.io.InputStream r2 = r2.getInputStream()     // Catch: java.lang.Throwable -> L4c
            r3.<init>(r2)     // Catch: java.lang.Throwable -> L4c
            java.io.BufferedReader r2 = new java.io.BufferedReader     // Catch: java.lang.Throwable -> L47
            r2.<init>(r3)     // Catch: java.lang.Throwable -> L47
            java.lang.String r1 = r2.readLine()     // Catch: java.lang.Throwable -> L45
            java.lang.String r4 = "x86"
            boolean r1 = r1.contains(r4)     // Catch: java.lang.Throwable -> L45
            if (r1 == 0) goto L39
            java.lang.String r1 = "i686"
        L34:
            java.lang.String r0 = a(r1)     // Catch: java.lang.Throwable -> L45
            goto L3e
        L39:
            java.lang.String r1 = java.lang.System.getProperty(r0)     // Catch: java.lang.Throwable -> L45
            goto L34
        L3e:
            r2.close()     // Catch: java.io.IOException -> L41
        L41:
            r3.close()     // Catch: java.io.IOException -> L65
            goto L65
        L45:
            r1 = move-exception
            goto L50
        L47:
            r2 = move-exception
            r5 = r2
            r2 = r1
            r1 = r5
            goto L50
        L4c:
            r2 = move-exception
            r3 = r1
            r1 = r2
            r2 = r3
        L50:
            java.lang.String r0 = java.lang.System.getProperty(r0)     // Catch: java.lang.Throwable -> L66
            java.lang.String r0 = a(r0)     // Catch: java.lang.Throwable -> L66
            r1.printStackTrace()     // Catch: java.lang.Throwable -> L66
            if (r2 == 0) goto L62
            r2.close()     // Catch: java.io.IOException -> L61
            goto L62
        L61:
        L62:
            if (r3 == 0) goto L65
            goto L41
        L65:
            return r0
        L66:
            r0 = move-exception
            if (r2 == 0) goto L6e
            r2.close()     // Catch: java.io.IOException -> L6d
            goto L6e
        L6d:
        L6e:
            if (r3 == 0) goto L73
            r3.close()     // Catch: java.io.IOException -> L73
        L73:
            goto L75
        L74:
            throw r0
        L75:
            goto L74
        */
        throw new UnsupportedOperationException("Method not decompiled: com.tencent.smtt.utils.b.a():java.lang.String");
    }

    public static String a(Context context) {
        try {
            return context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionName;
        } catch (Exception unused) {
            return null;
        }
    }

    public static String a(Context context, File file) {
        try {
            if (context.getApplicationContext().getPackageName().contains("com.jd.jrapp")) {
                TbsLog.i("AppUtil", "[AppUtil.getSignatureFromApk]  #1");
                String a2 = a(file);
                if (a2 != null) {
                    TbsLog.i("AppUtil", "[AppUtil.getSignatureFromApk]  #2");
                    return a2;
                }
            }
        } catch (Throwable unused) {
            TbsLog.i("AppUtil", "[AppUtil.getSignatureFromApk]  #3");
        }
        TbsLog.i("AppUtil", "[AppUtil.getSignatureFromApk]  #4");
        String a3 = a(context, file, false);
        TbsLog.i("AppUtil", "[AppUtil.getSignatureFromApk]  android api signature=" + a3);
        if (a3 == null) {
            a3 = a(file);
            TbsLog.i("AppUtil", "[AppUtil.getSignatureFromApk]  java get signature=" + a3);
        }
        if (a3 == null) {
            String a4 = a(context, file, true);
            TbsLog.i("AppUtil", "[AppUtil.getSignatureFromApk]  android reflection signature=" + a4);
            return a4;
        }
        return a3;
    }

    /* JADX WARN: Removed duplicated region for block: B:16:0x0035  */
    /* JADX WARN: Removed duplicated region for block: B:18:? A[RETURN, SYNTHETIC] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct code enable 'Show inconsistent code' option in preferences
    */
    private static java.lang.String a(android.content.Context r1, java.io.File r2, boolean r3) {
        /*
            r0 = 65
            if (r3 == 0) goto Ld
            java.lang.String r1 = r2.getAbsolutePath()
            android.content.pm.PackageInfo r1 = a(r1, r0)
            goto L19
        Ld:
            android.content.pm.PackageManager r1 = r1.getPackageManager()
            java.lang.String r2 = r2.getAbsolutePath()
            android.content.pm.PackageInfo r1 = r1.getPackageArchiveInfo(r2, r0)
        L19:
            r2 = 0
            if (r1 == 0) goto L32
            android.content.pm.Signature[] r3 = r1.signatures
            if (r3 == 0) goto L2b
            android.content.pm.Signature[] r3 = r1.signatures
            int r3 = r3.length
            if (r3 <= 0) goto L2b
            android.content.pm.Signature[] r1 = r1.signatures
            r3 = 0
            r1 = r1[r3]
            goto L33
        L2b:
            java.lang.String r1 = "AppUtil"
            java.lang.String r3 = "[getSignatureFromApk] pkgInfo is not null BUT signatures is null!"
            com.tencent.smtt.utils.TbsLog.w(r1, r3)
        L32:
            r1 = r2
        L33:
            if (r1 == 0) goto L39
            java.lang.String r2 = r1.toCharsString()
        L39:
            return r2
        */
        throw new UnsupportedOperationException("Method not decompiled: com.tencent.smtt.utils.b.a(android.content.Context, java.io.File, boolean):java.lang.String");
    }

    public static String a(Context context, String str) {
        String str2;
        try {
            try {
                return String.valueOf(Integer.toHexString(Integer.parseInt(String.valueOf(context.getPackageManager().getApplicationInfo(context.getPackageName(), 128).metaData.get(str)))));
            } catch (Exception unused) {
                return str2;
            }
        } catch (Exception unused2) {
            return null;
        }
    }

    private static String a(File file) {
        try {
            JarFile jarFile = new JarFile(file);
            byte[] bArr = new byte[8192];
            String a2 = a(a(jarFile, jarFile.getJarEntry("AndroidManifest.xml"), bArr)[0].getEncoded());
            Enumeration<JarEntry> entries = jarFile.entries();
            while (entries.hasMoreElements()) {
                JarEntry nextElement = entries.nextElement();
                String name = nextElement.getName();
                if (name != null) {
                    Certificate[] a3 = a(jarFile, nextElement, bArr);
                    String a4 = a3 != null ? a(a3[0].getEncoded()) : null;
                    if (a4 == null) {
                        if (!name.startsWith("META-INF/")) {
                            return null;
                        }
                    } else if (!a4.equals(a2)) {
                        return null;
                    }
                }
            }
            return a2;
        } catch (Exception e2) {
            e2.printStackTrace();
            return null;
        }
    }

    private static String a(String str) {
        return str == null ? "" : str;
    }

    private static String a(byte[] bArr) {
        int length = bArr.length;
        char[] cArr = new char[length * 2];
        for (int i = 0; i < length; i++) {
            byte b2 = bArr[i];
            int i2 = (b2 >> 4) & 15;
            int i3 = i * 2;
            cArr[i3] = (char) (i2 >= 10 ? (i2 + 97) - 10 : i2 + 48);
            int i4 = b2 & 15;
            cArr[i3 + 1] = (char) (i4 >= 10 ? (i4 + 97) - 10 : i4 + 48);
        }
        return new String(cArr);
    }

    private static Certificate[] a(JarFile jarFile, JarEntry jarEntry, byte[] bArr) {
        InputStream inputStream = jarFile.getInputStream(jarEntry);
        do {
        } while (inputStream.read(bArr, 0, bArr.length) != -1);
        inputStream.close();
        if (jarEntry != null) {
            return jarEntry.getCertificates();
        }
        return null;
    }

    public static int b(Context context) {
        try {
            return context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionCode;
        } catch (Exception unused) {
            return 0;
        }
    }

    public static String c(Context context) {
        if (TextUtils.isEmpty(a)) {
            try {
                return ((TelephonyManager) context.getSystemService("phone")).getDeviceId();
            } catch (Exception e2) {
                e2.printStackTrace();
                return "";
            }
        }
        return a;
    }

    public static String d(Context context) {
        if (TextUtils.isEmpty(b)) {
            try {
                return ((TelephonyManager) context.getSystemService("phone")).getSubscriberId();
            } catch (Exception e2) {
                e2.printStackTrace();
                return "";
            }
        }
        return b;
    }

    public static String e(Context context) {
        if (TextUtils.isEmpty(d)) {
            try {
                WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService("wifi");
                WifiInfo connectionInfo = wifiManager == null ? null : wifiManager.getConnectionInfo();
                return connectionInfo == null ? "" : connectionInfo.getMacAddress();
            } catch (Exception e2) {
                e2.printStackTrace();
                return "";
            }
        }
        return d;
    }

    public static String f(Context context) {
        if (TextUtils.isEmpty(e)) {
            try {
                e = Settings.Secure.getString(context.getContentResolver(), "android_id");
            } catch (Exception e2) {
                e2.printStackTrace();
            }
            return e;
        }
        return e;
    }
}
